<?php  
	session_start();
	
	/* Used for communication with the database */
	require_once("classes/Mail.class.php");
	include("classes/database.class.php");
	include("includes/define.php");
	$bd = new Database(MYSQL_DB_HOST,MYSQL_DB_LOGIN,MYSQL_DB_PASSWORD,MYSQL_DB_NAME);

	/* If someone try to go directly to this URL, redirect to index */
	if(!$_SESSION['admin'])
	{
		header("Location: http://".$_SERVER['SERVER_NAME']);
	}
	elseif(isset($_POST['result']))
	{
		require_once("classes/ArrayToCSV.class.php");
		require_once("classes/DataManager.class.php");
		$dm = new DataManager();
		$headers = $dm->makeHeaders();
		
		$atcsv = new ArrayToCSV("Resultats.csv",$headers,$dm->makeValues($_POST['studyId']));
		$atcsv->exportToCSV();
	}
	elseif(isset($_POST['results']))
	{
		require_once("classes/ArrayToCSV.class.php");
		require_once("classes/DataManager.class.php");
		$dm = new DataManager();
		$headers = $dm->makeHeaders();
		
		$atcsv = new ArrayToCSV("Resultats.csv",$headers,$dm->makeValues());
		$atcsv->exportToCSV();
	}
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
<title> Etudes réalisées </title>
</head>

<body>
	
	<div id="studies">
		<h1> Toutes les études </h1>
		
		<form method="post" action="admin.php">
			<input type="submit" name="return" value="Retour page admin"/>
		</form>

		<?php
			$DATABASE_isResult = $bd->query("SELECT * FROM participantsSessions;");
			$bd->close();
			if(mysql_num_rows($DATABASE_isResult) != 0)
			{
				echo "<br/>
					<form method='post' action='oldStudies.php'>
						<input type='submit' name='results' value='Obtenir tous les résultats'/>
					</form>";
			}
		?>
		
		<br/>
		<hr/>
		
		<?php
			$DATABASE_allStudies = $bd->query("SELECT * FROM studies ORDER BY studyId DESC;");
			$bd->close();
			if(mysql_num_rows($DATABASE_allStudies) > 0)
			{
				while($DATABASE_Study = mysql_fetch_assoc($DATABASE_allStudies))
				{
					
					if($DATABASE_Study['endDate'] == NULL)
					{
						$beginDate = preg_replace("/(\d+)-(\d+)-(\d+)/", "$3/$2/$1", $DATABASE_Study['beginDate']);
						echo "<br/><h4 class='underline'>Etude en cours depuis le ".$beginDate."</h4>";
					}
					else 
					{
						$beginDate = preg_replace("/(\d+)-(\d+)-(\d+)/", "$3/$2/$1", $DATABASE_Study['beginDate']);
						$endDate = preg_replace("/(\d+)-(\d+)-(\d+)/", "$3/$2/$1", $DATABASE_Study['endDate']);
						echo "<br/><h4 class='underline'>Etude du ".$beginDate." au ".$endDate."</h4>";
					}
						
					$DATABASE_allSessions = $bd->query("SELECT * FROM sessions WHERE studyId = '".$DATABASE_Study['studyId']."' ORDER BY date DESC;");
					$bd->close();
					if(mysql_num_rows($DATABASE_allSessions) > 0)
					{
						$DATABASE_isResult = $bd->query("SELECT * FROM participantsSessions WHERE sessionId IN (SELECT sessionId FROM sessions WHERE studyId = '".$DATABASE_Study['studyId']."');");
						if(mysql_num_rows($DATABASE_isResult))
						{
							echo " <br/>
								<form method='post' action='oldStudies.php'>
									<input type='hidden' name='studyId' value='".$DATABASE_Study['studyId']."'/>
									<input type='submit' name='result' value='Obtenir les résultats de cette étude'/>
								</form>
								<br/>";
						}
						
						echo "<table> <thead> <tr> <td>Date</td> <td>Induction</td> <td>Code</td> </tr> </thead> <tbody>";
						while($DATABASE_Session = mysql_fetch_assoc($DATABASE_allSessions))
						{
							$userDate = preg_replace("/(\d+)-(\d+)-(\d+)/", "$3/$2/$1", $DATABASE_Session['date']);
							echo "<tr> <td>".$userDate."</td><td>".$DATABASE_Session['induction']."</td><td>".$DATABASE_Session['sessionId']."</td></tr>";
						}
						echo "<tbody> </table>";
					}
					else { echo "<p>Aucune session réalisée pour cette étude</p>"; }
				}
			}
			else { echo "<p>Aucune étude n'a été réalisée</p>"; }
		?>
	</div>
	
</body>
</html>